package cn.gdcp.service;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button btnStart;
    private Button btnStop;
    private Button btnBind;
    private Button btnUnBind;
    private ServiceConnection serviceConnection;
    private MyBinder binder;
    private Button btnAdd;
    private Button btn_toUpperCase;
    private Button showTime;
    private Button reverseOrder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //服务连接者
        serviceConnection=new ServiceConnection() {
            @Override
            //componentName 连接主键的名字
            //iBinder等于Service返回的binder
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                Log.e("MainActivity","onServiceConnected");
                binder=(MyBinder)iBinder;
            }
            @Override
            public void onServiceDisconnected(ComponentName componentName) {
                Log.e("MainActivity","onServiceDisconnected");
            }
        };

        btnStart=findViewById(R.id.start_service);
        btnStop=findViewById(R.id.stop_service);
        btnBind=findViewById(R.id.bind_service);
        btnUnBind=findViewById(R.id.unbind_service);
        btnAdd=findViewById(R.id.btn_Add);
        btn_toUpperCase=findViewById(R.id.btn_toUpperCase);
        showTime=findViewById(R.id.show_time);
        reverseOrder=findViewById(R.id.reverse_order);

        //启动服务
        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(MainActivity.this,MyService.class);
                startService(intent);
            }
        });
        //停止、销毁服务, 停止服务后启动服务之前要重新创建；取消绑定后再停止
        btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setClass(MainActivity.this,MyService.class);
                stopService(intent);
            }
        });
        //绑定服务
        btnBind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setClass(MainActivity.this,MyService.class);
                bindService(intent,serviceConnection,BIND_AUTO_CREATE);
            }
        });
        //取消绑定服务
        btnUnBind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                unbindService(serviceConnection);
            }
        });
        //计算两个数的和
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int result=binder.add(100,200);
                Log.e("binder.add","result="+result);
            }
        });
        //转换大写字母
        btn_toUpperCase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str=binder.toUpperCase("hello world");
                Log.e("binder.toUpperCase","str="+str);
            }
        });
        //获取当前时间
        showTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String time=binder.getTimeStr();
                Log.e("binder.getTimeStr","time="+time);
            }
        });
        //字符串单词逆序
        reverseOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str=binder.strRereverse("I Love Android");
                Log.e("binder.strRereverse","str="+str);
            }
        });

    }
}
